package com.kang.util;

import com.kang.entity.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {

    //构建树形结构
    public static List<SysMenu> buildTree(List<SysMenu> menuList) {
        //封装最终集合数据
        List<SysMenu> resultList = new ArrayList<>();
        //遍历所有菜单集合
        for (SysMenu sysMenu : menuList) {
            if (sysMenu.getParentId() == 0) {
                resultList.add(findChildren(sysMenu,menuList));
            }
        }
        return resultList;
    }

    //从根节点查询子节点  是子节点就进行数据封装
    public static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> menuList) {
        //数据初始化
        sysMenu.setChildren(new ArrayList<>());
        //遍历递归查找
        for (SysMenu menu : menuList) {
            if (sysMenu.getId().equals(menu.getParentId())) {
                sysMenu.getChildren().add(findChildren(menu,menuList));
            }
        }
        return sysMenu;
    }
}
